

float fVsrc = 0;
float fVldr = 0;
bool bVsrcLow = false;
bool bVldrLow = false;



void setup()
{
  // set pins 2 to 13 as low outputs to save power (leaves RX, TX alone)
  for (unsigned char i = 2; i <= 13; i++)
  {
      pinMode(i, OUTPUT);
      digitalWrite(i, LOW);
  }
  
  // setup debug serial 
  Serial.begin(57600);
  
  // read initial input values
  fVsrc = 15.0f * analogRead(0) / 1024.0f;
  fVldr = 3.3f * analogRead(1) / 1024.0f;
}

void loop()
{
  // read inputs
  fVsrc = fVsrc*0.99 + (15.0f * analogRead(0) / 1024.0f)*0.01;
  fVldr = fVldr*0.99 + (3.3f * analogRead(1) / 1024.0f)*0.01;

  // detect input states
  if (fVsrc < 10)
  {
      bVsrcLow = true;
  }
  else if (fVsrc > 10.5)
  {
      bVsrcLow = false;
  }
  
  if (fVldr > 0.2f)
  {
      bVldrLow = false;
  }
  else if (fVldr < 0.1f)
  {
      bVldrLow = true;
  }

  // set lighting
  if (bVsrcLow == true)
  {
      analogWrite(3, 0);
      digitalWrite(13, (millis() / 200) % 2);
  }
  else if (bVldrLow == false)
  {
      analogWrite(3, 0);
      digitalWrite(13, (millis() / 1000) % 2);
  }
  else
  {
      analogWrite(3, 255);
      digitalWrite(13, 1);
  }

  // debug  
  Serial.print("Vsrc=");
  Serial.print(fVsrc);
  Serial.print(", Vldr=");
  Serial.println(fVldr);
  
  delay(50);
}
